<?php
// 获取订单信息
// header("content-type:text/html;charset=utf-8");
include_once("conn.php");

$uid = $_SESSION['USERINFOS']['uid'];
$c = 0;
function get_order_list(){
    global $conn;
    global $uid;
    global $c;
    $sql = "SELECT * FROM tb_order_info 
            LEFT JOIN tb_goods_info 
            ON tb_order_info.g_id = tb_goods_info.g_id 
            WHERE tb_order_info.uid = '".$uid."'
            AND o_status != 2 
            AND o_status != -1 
            ORDER BY tb_order_info.id DESC";
    // echo $sql;

    $btn='';
    $form = '';


    $result = mysqli_query($conn,$sql);
    if ($result) {
        $c = mysqli_num_rows($result);
        if ($c > 0) {
            while ($row = mysqli_fetch_array($result)) {
                if ($row['o_status'] == 0) {
                    $row['o_status'] = "订单待发货";
                    $btn = "    
                    <button class='btn btn-outline-primary btnstyle' onclick='change_info(".$row[0].")'>修改信息</button>
                    <a href='php/cancel_order.php?id=".$row[0]."'><button class='btn btn-outline-danger btnstyle'>取消订单</button></a>
                    ";
                    $form = "
                    <div class='change_form' id='".$row[0]."' style='display: none;'>
                        <form class='row g-3' method='post' action='php/change_order.php'>
                            <input type='hidden' name='oid' value='".$row['o_id']."'>
                            <div class='col-md-2'>
                                <input type='text' class='form-control' id='name' name='name' value='".$row['o_name']."' placeholder='请输入收货人姓名' required>
                            </div>
                            <div class='col-md-3'>
                                <input type='text' class='form-control' id='address' name='address' value='".$row['o_address']."' placeholder='请输入收货地址' required>
                            </div>
                            <div class='col-md-3'>
                                <input type='text' class='form-control' id='phone' name='phone' value='".$row['o_phone']."' placeholder='请输入手机号' required>
                            </div>
                            <div class='col-md-2'>
                                <button type='submit' class='btn btn-primary' onclick='return change_order(".$row[0].");'>提交修改</button>
                            </div>
                        </form>
                    </div>
                    ";
                }elseif ($row['o_status'] == 1) {
                    $row['o_status'] = "订单已发货";
                    $btn = "<a href='php/over_order.php?id=".$row[0]."'><button class='btn btn-outline-success btnstyle'>确认收货</button></a>";
                }
                echo"
                <div class='order_item'>
                    <div class='order_item_img'>
                        <img src='../goods_imgs/".$row['g_img']."'>
                    </div>
                    <div class='order_item_info'>
                        <p class='lead'>".$row['g_name']."</p>
                        <p class='info_p'><span class='info_title'>订单编号：</span>".$row['o_id']."</p>
                        <p class='info_p'><span class='info_title'>收货人：</span>".$row['o_name']."</p>
                        <p class='info_p'><span class='info_title'>收货地址：</span>".$row['o_address']."</p>
                        <p class='info_p'><span class='info_title'>联系方式：</span>".$row['o_phone']."</p>
                        <p class='info_p'><span class='info_title'>购买数量：</span>".$row['o_num']."</p>
                        <p class='info_p'><span class='info_title'>商品单价：</span>".$row['o_price']."</p>
                        <p class='info_p'><span class='info_title'>交易金额：</span>".$row['o_pay']." 元</p>
                        <p class='info_p'><span class='info_title'>下单时间：</span>".$row['o_begintime']."</p>
                        <p class='info_p'><span class='info_title'>订单状态：</span>".$row['o_status']."</p>
                    </div>
                    <div class='order_item_btn'>".$btn."</div>
                </div>".$form;
            }
        }else{
            echo "<div class='none_order'><img src='img/null-order.svg'></div>";
        }
    }
}
